package com.tianji.promotion.mq;

import com.tianji.common.constants.MqConstants;
import com.tianji.promotion.domain.dto.UserCouponDTO;
import com.tianji.promotion.service.IUserCouponService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@RequiredArgsConstructor
public class CouponReceiveListener {
    private final IUserCouponService userCouponService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "coupon.receive.queue", durable = "true"),
            exchange = @Exchange(value = MqConstants.Exchange.Promotion_EXCHANGE,type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.COUPON_RECEIVE
    ))
    public void onMsg(UserCouponDTO uc){
        if(uc.getSerialNum() == null){
            // 没有兑换码
            userCouponService.updateCouponAndCodeNew(uc.getCouponId(),uc.getUserId());
        }else{
            // 有兑换码
            userCouponService.updateCouponAndCode(uc.getCouponId(),uc.getSerialNum(),uc.getUserId());
        }
    }
}
